<?php 
/**
* 
*/
class EmployeeController extends CI_Controller
{
	
	function __construct()
	{
		parent::__construct();
		$this->load->model('Employee_Model');
		$this->load->model('Reference_Model');
		$this->load->model('Role_Model');
	}

	public function index(){

		// use database 2 //
		// $this->answer = $this->load->database('db2', true);
		// $this->answer->select ('*');
		// $this->answer->from ('word');
		// $query = $this->answer->get();
		// $result = $query->row_array();
		// echo $result['word'];
		// ---------------------------- //

		$data['resultEmployee'] = $this->Employee_Model->getAllEmployee();

		$this->load->view("include/header");
		$this->load->view("employee/index",$data);
		$this->load->view("include/footer");


	}

	public function changePassword(){
		$this->load->view("include/header");
		$this->load->view("employee/changePassword");
		$this->load->view("include/footer");
	}

	public function createEmployee(){

		$data['resultTitle'] = $this->Reference_Model->getAllTitle();
		$data['resultPosition'] = $this->Reference_Model->getAllPosition();
		$data['resultDepartment'] = $this->Reference_Model->getAllDepartment();
		$data['resultSubdepartment'] = $this->Reference_Model->getAllSubDepartment();
		$data['resultRole'] = $this->Role_Model->getAllRole();

		$this->load->view("include/header");
		$this->load->view("Employee/create",$data);
		$this->load->view("include/footer");
	}

	public function editEmployee($id){	

		$data['resultTitle'] = $this->Reference_Model->getAllTitle();
		$data['resultPosition'] = $this->Reference_Model->getAllPosition();
		$data['resultDepartment'] = $this->Reference_Model->getAllDepartment();
		$data['resultSubdepartment'] = $this->Reference_Model->getAllSubDepartment();
		$data['resultRole'] = $this->Role_Model->getAllRole();

		$data['dataUser'] = $this->Employee_Model->getUser($id)->row_array();

		$this->load->view("include/header");
		$this->load->view("Employee/edit",$data);
		$this->load->view("include/footer");
	}

	public function checkOldPassword(){
		$session_data = $this->session->userdata('userdata');
		$userData = $session_data['UserInfo'];

		$rs = $this->Employee_Model->checkPasswordOfID($this->input->post('oldPass'),$userData['id']);
		if($rs->num_rows > 0){
			echo 'TRUE';
		}else{
			echo 'FALSE';
		}
	}

	public function updateNewPassword(){
		$session_data = $this->session->userdata('userdata');
		$userData = $session_data['UserInfo'];
		$this->Employee_Model->changePassword($userData['id'],$this->input->post('newPass'));
	}

	public function checkNewpassWithOldpass(){
		$session_data = $this->session->userdata('userdata');
		$userData = $session_data['UserInfo'];

		$rs = $this->Employee_Model->checkNewPasswordWithOldPassword($this->input->post('newPassword'),$userData['id']);
		if($rs){
			echo 'TRUE';
		}else{
			echo 'FALSE';
		}
	}

	public function changeStatus($id,$status){
		$s = "";
		if ($status == 'A') {
			$s = 'N';
		}else{
			$s = 'A';
		}
		$this->Employee_Model->changeStatus($id,$s);
		redirect('EmployeeController','refresh');
	}

	public function insertEmployee(){

		$newid = "E" . sprintf( '%02s' , ((int)substr( $this->Employee_Model->getLastEmployeeID() , 1) + 1) );
		$newemp = array(
				'id' => $newid,
				'emp_title' => $this->input->post('title'),
				'emp_name' => iconv('UTF-8','TIS-620', $this->input->post('firstname')),
				'emp_lastname' => iconv('UTF-8','TIS-620', $this->input->post('lastname')),
				'emp_role' => $this->input->post('role'),
				'emp_position_code' => $this->input->post('position'),
				'emp_department_code' => $this->input->post('department'),
				'emp_subdepartment_code' => $this->input->post('subdepartment'),
				'emp_status' => $this->input->post('status'),
				'emp_user' => $this->input->post('username'),
				'emp_pass' => md5($this->input->post('password')),
				'login_failcount' => 0
			);

		if($this->input->post("amcam") != ""){
			$newemp += array('amcam_user' =>  $this->input->post("amcam"));
		}

		if($this->input->post("amcats") != ""){
			$newemp += array('amcats_user' =>  $this->input->post("amcats"));
		}

		if($this->input->post("tel") != ""){
			$newemp += array('emp_tel' =>  $this->input->post("tel"));
		}

		$this->Employee_Model->insert($newemp);
		echo "success";
	}

	public function updateEmployee(){

		$newemp = array(
				'emp_title' => $this->input->post('title'),
				'emp_name' => iconv('UTF-8','TIS-620', $this->input->post('firstname')),
				'emp_lastname' => iconv('UTF-8','TIS-620', $this->input->post('lastname')),
				'emp_position_code' => $this->input->post('position'),
				'emp_department_code' => $this->input->post('department'),
				'emp_subdepartment_code' => $this->input->post('subdepartment'),
				'emp_role' => $this->input->post('role'),
				'emp_status' => $this->input->post('status')
			);

		if($this->input->post("amcam") != ""){
			$newemp += array('amcam_user' =>  $this->input->post("amcam"));
		}

		if($this->input->post("amcats") != ""){
			$newemp += array('amcats_user' =>  $this->input->post("amcats"));
		}

		if($this->input->post("tel") != ""){
			$newemp += array('emp_tel' =>  $this->input->post("tel"));
		}

		$this->Employee_Model->update($this->input->post('id'),$newemp);
		echo "success";
	}

	public function checkUsername(){
		$rs = $this->Employee_Model->checkUser(trim($this->input->post('username')));

		echo $rs->num_rows ;
		if ($rs->num_rows > 0) {
			echo 'haveUser';
		}else{
			echo 'notFound';
		}
	}


}
?>